package com.wy.business.manage.scenery.actions;

import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ExceptionMapping;
import org.apache.struts2.convention.annotation.ExceptionMappings;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.wy.business.manage.scenery.pojos.TCampusSceneryAtlas;
import com.wy.business.manage.scenery.pojos.TCampusSceneryPicture;
import com.wy.business.manage.scenery.services.CampusSceneryService;
import com.wy.framework.actions.AbstractBaseAction;
import com.wy.framework.dao.ErrorSqlException;
import com.wy.framework.exceptions.ServiceException;


@ParentPackage("json")
@Namespace("/framework/campusscenery")
@ExceptionMappings({ @ExceptionMapping(exception = "java.lange.RuntimeException", result = "error") })
public class CampusSceneryAction extends AbstractBaseAction {
	private static final long serialVersionUID = 1L;
	private static Log logger = LogFactory.getLog(CampusSceneryAction.class);
	private List<Map> rows;

	@Autowired
	private CampusSceneryService campusSceneryService;
	
	
	private TCampusSceneryAtlas atlas;
	
	private TCampusSceneryPicture picture;
	
	
	private String pppid;
	public String getPppid() {
		return pppid;
	}

	public void setPppid(String pppid) {
		this.pppid = pppid;
	}

	private String atlasIds;
	private String pictureUrls;	
	private String pictureNames;
	private String atlasName;
	public String getAtlasName() {
		return atlasName;
	}

	public void setAtlasName(String atlasName) {
		this.atlasName = atlasName;
	}

	private int i;
	
	public int getI() {
		return i;
	}

	public void setI(int i) {
		this.i = i;
	}

	@Action(value = "queryAtlas", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false","root","rows"}) }) 
	public String queryAtlas() throws ServiceException {
		int pageIndex=Integer.parseInt(ServletActionContext.getRequest().getParameter("pageIndex"));
		rows = campusSceneryService.queryAtlas(pageIndex,atlasName);
		return SUCCESS;
	}

	@Action(value = "addAtlas", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) }) 
	public String addAtlas() throws ErrorSqlException{
		campusSceneryService.addAtlas();
		return SUCCESS;
	}
	
	@Action(value = "modifyAtlas", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) }) 
	public String modifyAtlas() throws ErrorSqlException{
		campusSceneryService.modifyAtlas(atlas);
		return SUCCESS;
	}
	
	@Action(value = "removeAtlas", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
					"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) }) 
	public String removeAtlas() throws ErrorSqlException{
		campusSceneryService.removeAtlas(atlas);
		return SUCCESS;
	}
	
	@Action(value = "queryPicture", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false","root","rows"}) }) 
	public String queryPicture() throws ErrorSqlException{
		rows=campusSceneryService.queryPicture(pppid);
		//System.out.print(rows);
		return SUCCESS;
	}
	@Action(value = "searchPicture", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false","root","rows"}) }) 
	public String searchPicture() throws ErrorSqlException{
		rows=campusSceneryService.searchPicture(pictureNames);
		//System.out.print(rows);
		return SUCCESS;
	}
	
	@Action(value = "removePicture", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) }) 
	public String removePicture() throws ErrorSqlException{
		campusSceneryService.removePicture(picture);
		return SUCCESS;
	}
	
	@Action(value = "modifyPicture", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) }) 
	public String modifyPicture() throws ErrorSqlException{
		picture.setAtlas(atlas);
		campusSceneryService.modifyPicture(picture);
		return SUCCESS;
	}
	
	@Action(value = "addPicture", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false"}) }) 
	public String addPicture() throws ErrorSqlException, URISyntaxException{
		//String a=ServletActionContext.getRequest().getParameter("A");
		campusSceneryService.addPicture(atlasIds,pictureUrls,pictureNames);
		return SUCCESS;
	}
	
	@SuppressWarnings("unchecked")
	@Action(value = "queryPictureById", 
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false","root","rows"}) }) 
	public String queryPictureById() throws ErrorSqlException{
		rows=campusSceneryService.queryPictureById();
		Map a=new HashMap();
		a.put("atlasList", campusSceneryService.queryAtlas());
		rows.add(a);
		return SUCCESS;
	}
	
	@Action(value = "queryAtlasLists",
			interceptorRefs = { @InterceptorRef("sessionStack") }, 
			results = { @Result(name = "success", type = "json", params = {
			"noCache", "true", "contentType", "text/html", "ignoreHierarchy",
			"false","root","rows"}) }) 
	public String queryAtlasLists() throws ErrorSqlException{
		rows=campusSceneryService.queryAtlas();
		return SUCCESS;
	}

	public List<Map> getRows() {
		return rows;
	}

	public void setRows(List<Map> rows) {
		this.rows = rows;
	}

	public TCampusSceneryAtlas getAtlas() {
		return atlas;
	}

	public void setAtlas(TCampusSceneryAtlas atlas) {
		this.atlas = atlas;
	}

	public TCampusSceneryPicture getPicture() {
		return picture;
	}

	public void setPicture(TCampusSceneryPicture picture) {
		this.picture = picture;
	}

	public String getAtlasIds() {
		return atlasIds;
	}

	public void setAtlasIds(String atlasIds) {
		this.atlasIds = atlasIds;
	}

	public String getPictureUrls() {
		return pictureUrls;
	}

	public void setPictureUrls(String pictureUrls) {
		this.pictureUrls = pictureUrls;
	}

	public String getPictureNames() {
		return pictureNames;
	}

	public void setPictureNames(String pictureNames) {
		this.pictureNames = pictureNames;
	}

}
